// pages/userInformation/userInformation.js

//获取app实例
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    headImage: null,
    nickname: '请设置个性昵称',
    mobile: '',
    sex: ['女', '男'], //性别
    sexSelected: -1, //-1未设置性别，0男，1女
    birth: '', //出生日期
    currentDate: null
  },

  onLoad: function(){
    var date = new Date();
    this.setData({
      currentDate: date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function () {
    console.log(new Date())
    const userId = app.globalData.userId;
    if (userId) {
      wx.request({
        url: app.baseURL + 'Mine&a=index&user_id=' + userId,
        success: (res) => {
          console.log(res)
          const userData=res.data.member_data;
          this.setData({
            nickname: userData.nickname,
            mobile: userData.mobile,
            sexSelected: userData.sex,
            birth: userData.birthday,
            headImage: 'https://try.0791jr.com/data/attachment/avatar/' + userData.avatar,
          })
        }
      })
    } 
  },

  //更换头像
  clickChangeImage(){
    const userId = app.globalData.userId;
    wx.chooseImage({
      count: 1, //只能选中1张
      sourceType: ['album', 'camera'], //album相册选图，camera 使用相机，默认二者都有
      success: (res) => {
        let tempFilePaths = res.tempFilePaths[0];
        console.log(tempFilePaths);
        wx.uploadFile({
          url: app.baseURL + 'Mine&a=set_photo', //仅为示例，非真实的接口地址
          filePath: tempFilePaths,
          name: 'file',
          formData: {
            user_id: userId 
          },
          success: (res) => {
            let data = JSON.parse(res.data);//由于拿到的数据未解析，这里先解析json
            if (data.status == 2) {
              wx.showToast({ title: '头像上传成功', icon: 'none' });
              this.setData({
                headImage: tempFilePaths
              })
            }
          }
        })
      }
    });
  },

  //设置性别
  clickActionSheet(){
    wx.showActionSheet({
      itemList: ['女', '男'],
      success: (res) => {
        const userId = app.globalData.userId;
        if (userId) {
          wx.request({
            url: app.baseURL + 'Mine&a=set_sex&user_id=' + userId + '&sex=' + res.tapIndex,
            success:(res)=>{
              let data = res.data;
              if (data.str === 1) {
                wx.showToast({ title: '设置成功',icon: 'none' });
              }
            }
          })
        }
        this.setData({
          sexSelected: res.tapIndex
        })
      },
    })
  },

  //设置生日
  clickBirth (e){
    const userId = app.globalData.userId;
    wx.request({
      url: app.baseURL + 'Mine&a=set_date&user_id=' + userId + '&birthday=' + e.detail.value,
      success: (res) => {
        let data = res.data;
        if (data.str === 1) {
          wx.showToast({
            title: '设置成功',
          });
          wx.redirectTo({
            url: '',
          })
        }
      }
    })
    this.setData({
      birth: e.detail.value
    })
  }
})